package com.seadragon.app.ksr.util;

import java.nio.ByteBuffer;

import com.seadragon.app.ksr.model.LatLng;

public class BinUtils {
	public static double getDouble(byte[] bbuf, int offset, boolean littleEndian) {
		ByteBuffer dbuf = null;
		if (littleEndian) {
			dbuf = ByteBuffer.allocate(8);
			for (int i = 0; i < 8; i++) {
				dbuf.put(i, bbuf[offset + 7 - i]);
			}
		} else {
			dbuf = ByteBuffer.wrap(bbuf, offset, 8);
		}
		return dbuf.getDouble();
	}

	public static LatLng[] convertToDoubleArray(byte[] bbuf) {
		int count = bbuf.length / 16;
		LatLng[] points = new LatLng[count];
		int offset = 0;
		for (int i = 0; i < count; i++) {
			LatLng point = new LatLng();
			point.setLng(getDouble(bbuf, offset, true));
			offset += 8;
			point.setLat(getDouble(bbuf, offset, true));
			points[i] = point;
			offset += 8;
		}
		return points;
	}

}
